Imports Clo = Enhancements.Core.Logging
Imports Ere = Enhancements.Basic.RegularExpressions
Imports Exe = Enhancements.Basic.Expressions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Iou = System.IO
Imports Ref = System.Reflection
Imports Reg = System.Text.RegularExpressions
Imports Sys = System
Imports Tex = System.Text
Imports Xsd = System.Xml.Schema
Imports W3c = System.Xml

Namespace Enhancements.Basic.Expressions.Values
    ''' <summary>Represents a token extracted from an expression that has a numeric value.</summary>
    <Serializable(), DebuggerStepThrough()> _
    Public MustInherit Class Number
        Inherits Value

        Friend Sub New(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByVal _expressionEndIndex As Sys.Int32, ByVal _value As Sys.Object)
            MyBase.New(_expression, _expressionBeginIndex, _expressionEndIndex, _value)
        End Sub

        Private Shared Function __Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32) As Sys.Object
            Dim _returnValue As Sys.Object = Nothing
            Dim _decimal As [Decimal] = Nothing
            Dim _integer As [Integer] = Nothing
            If [Decimal].TryParse(_expression, _expressionBeginIndex, _decimal) Then
                _returnValue = _decimal
            Else
                If [Integer].TryParse(_expression, _expressionBeginIndex, _integer) Then
                    _returnValue = _integer
                End If
            End If
            If Assert.IsNull(_returnValue, "_returnValue", False) Then
                _returnValue = NumberParseException.___Create(_expression, _expressionBeginIndex)
            End If

            Return _returnValue
        End Function

        ''' <summary>Parses an expression for a numeric value starting by a begin index.</summary>
        ''' <param name="_expression">Expression to parse.</param>
        ''' <param name="_expressionBeginIndex">Expression begin index to use.</param>
        ''' <exception cref="NumberParseException">The operation did not produce a valid token.</exception>
        ''' <returns> </returns>
        Public Overloads Shared Function Parse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32) As Number
            Dim _returnValue As Sys.Object = __Parse(_expression, _expressionBeginIndex)
            If Assert.IsOfType(_returnValue, "_returnValue", GetType(NumberParseException), False) Then
                NumberParseException.___Throw(CType(_returnValue, NumberParseException))
            End If

            Return CType(_returnValue, Number)
        End Function

        ''' <summary>Tries to parse an expression for a numeric value starting by a begin index.</summary>
        ''' <param name="_expression">Expression to parse.</param>
        ''' <param name="_expressionBeginIndex">Expression begin index to use.</param>
        ''' <param name="_result">The result of the operation if possible. If not, null is returned.</param>
        ''' <returns> </returns>
        Public Overloads Shared Function TryParse(ByVal _expression As System.String, ByVal _expressionBeginIndex As Sys.Int32, ByRef _result As Number) As System.Boolean
            Dim _returnValue As System.Boolean = False
            Dim _dummy As Sys.Object = __Parse(_expression, _expressionBeginIndex)
            If Assert.IsOfType(_dummy, "_dummy", GetType(NumberParseException), False) Then
                _result = Nothing
                _returnValue = False
            Else
                _result = CType(_dummy, Number)
                _returnValue = True
            End If

            Return _returnValue
        End Function

        Private Shared __Types() As System.Type = Nothing
        Friend Overloads Shared Function ___TypesGet() As System.Type()
            If __Types.IsNull("__Types", False) Then
                Dim _typesList As Gen.List(Of System.Type) = New Gen.List(Of System.Type)()
                _typesList.Add(GetType([Decimal]))
                _typesList.Add(GetType([Integer]))
                __Types = _typesList.ToArray()
            End If

            Return __Types
        End Function

        Friend Overloads Shared Function ___AssertIsValidValue(ByVal _value As Sys.Object) As System.Boolean
            Dim _returnValue As System.Boolean = [Decimal].___AssertIsValidValue(_value) Or [Integer].___AssertIsValidValue(_value)

            Return _returnValue
        End Function

        Friend Overloads Shared Function ___AssertIsValidType(ByVal _type As Sys.Type) As System.Boolean
            Dim _returnValue As System.Boolean = False
            If _type.IsNullNot("_type", False) Then
                If ___TypesGet().Contains(_type) Then
                    _returnValue = True
                End If
            End If

            Return _returnValue
        End Function

        Friend Overloads Shared Function ___AssertIsValidTypeAndValue(ByVal _type As Sys.Type, ByVal _value As Sys.Object) As System.Boolean
            Dim _returnValue As System.Boolean = False
            If ___AssertIsValidType(_type) Then
                If _type Is GetType([Decimal]) AndAlso [Decimal].___AssertIsValidValue(_value) Then
                    _returnValue = True
                Else
                    If _type Is GetType([Integer]) AndAlso [Integer].___AssertIsValidValue(_value) Then
                        _returnValue = True
                    End If
                End If
            End If

            Return _returnValue
        End Function

    End Class
End Namespace
